<?php

class model_pddLogistics extends L {

    /**
     * 描述 ：分页的查询语句拼接
     * 作者 ：Zoro.Zhu
    **/
    public static function searchSql($params = array()){
        $sql = "SELECT
             `id`, `code`, `logisticsCompany`, `available`
        FROM
            `pdd_logistics`
        WHERE 1";
        
        $sql .= !empty($params['search']['id']) ? " AND `id` = '{$params['search']['id']}' " : "";
        $sql .= !empty($params['search']['code']) ? " AND `code` = '{$params['search']['code']}' " : "";
        $sql .= !empty($params['search']['logisticsCompany']) ? " AND `logisticsCompany` LIKE '%{$params['search']['logisticsCompany']}%' " : "";
        $sql .= !empty($params['search']['available']) ? " AND `available` = '{$params['search']['available']}' " : "";
        return $sql;
    }

    /**
     * 描述 ：执行修改数据的sql
     * 作者 ：Zoro.Zhu
    **/
    public static function updateSql($data = array()){
        $sql = "UPDATE `pdd_logistics` 
        SET 
            `code` = '{$data['code']}',
            `logisticsCompany` = '{$data['logisticsCompany']}',
            `available` = '{$data['available']}'
        WHERE
            `id` = '{$data['id']}'";
        return L::sql($sql);
    }

    /**
     * 描述 ：执行插入数据的sql
     * 作者 ：Zoro.Zhu
    **/
    public static function insertSql($data = array()){
        $sql = "INSERT INTO `pdd_logistics`(
             `code`, `logisticsCompany`, `available`
        ) VALUES (
             '{$data['code']}', '{$data['logisticsCompany']}', '{$data['available']}'
        )";
        return L::sql($sql);
    }

    /**
     * 描述：根据主键获取数据
     * 作者：Zoro.Zhu
    **/
    public static function getKeyInfo($key){
        $sql = "SELECT
             `id`, `code`, `logisticsCompany`, `available`
        FROM
            `pdd_logistics`
        WHERE 
            `id` = '{$key}'";
        return L::sql($sql);
    }

    /**
     * 描述 ：生成一份物流列表
     * 作者 ：Zoro.Zhu
    **/
    public static function getList(){
        $key = 'pddLogisticsList';
        if (of_base_com_kv::get($key) !== false) {
            $data = of_base_com_kv::get($key);
        } else {
            $info = L::sql("SELECT
                 `id`, `code`
            FROM
                `pdd_logistics`
            WHERE `available` = 1");
            $data = [];
            foreach($info as $row){
                $data[$row['code']] = $row['id'];
            }
            $data = json_encode($data);
            of_base_com_kv::set($key , $data, 86400);
        }
        $data = json_decode($data, true);
        return $data;

    }

    /**
     * 描述 ：生成一份物流列表
     * 作者 ：Zoro.Zhu
     **/
    public static function getListName(){
        $key = 'pddLogisticNamesList';
        if (of_base_com_kv::get($key) !== false) {
            $data = of_base_com_kv::get($key);
        } else {
            $info = L::sql("SELECT
                 `id`, `logisticsCompany`
            FROM
                `pdd_logistics`
            WHERE `available` = 1");
            $data = [];
            foreach($info as $row){
                $data[$row['logisticsCompany']] = $row['id'];
            }
            $data = json_encode($data);
            of_base_com_kv::set($key , $data, 86400);
        }
        $data = json_decode($data, true);
        return $data;

    }

}